package com.inspur.cloud.aspect;

import com.inspur.cloud.entity.dataspace.UserEntity;
import com.inspur.cloud.entity.dataspace.UserThreadLocal;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.util.XssSqlHttpServletRequestWrapper;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "logFilter", urlPatterns = "/dataspace/api/v1/*")
public class LogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        XssSqlHttpServletRequestWrapper request = new XssSqlHttpServletRequestWrapper((HttpServletRequest)servletRequest);
        //存储request的请求体
        UserThreadLocal.setValue("requestBody",request.getRequestParams());
        UserEntity user = AuthorizationHelper.getUser();
        if(user!=null) {
            //存储登录用户名和id
            UserThreadLocal.setValue("username",user.getName());
            UserThreadLocal.setValue("userId",user.getId());
        }
        filterChain.doFilter(request,servletResponse);
    }
    @Override
    public void destroy() {

    }
}
